// main foc 24MHz
#include <STC8G_H_Delay.h>
#include <Config.h>
#define USB1 P10
#define USB2 P11
#define TF P13
#define GPIO P14
#define KEY P32
#define SEL P33

void main()
{
    bit Usb1State, Usb2State;
    int i;
    P1M0 = 0x00;
    P1M1 = 0xe7;

    P3M0 = 0x00;
    P3M1 = 0xf3;
    WDT_CONTR = 0x24;
    TF = 1;
    if (USB1 == 1)
        SEL = 0;
    else
        SEL = 1;
    Usb1State = USB1;
    Usb2State = USB2;
    while (1)
    {
        WDT_CONTR = 0x34;
        if (KEY == 0)
        {
            i++;
            delay_ms(10);
            if (i > 200)
                TF = 0;
            WDT_CONTR = 0x34;
        }
        else
        {
            if (i > 2 & i < 100)
            {
                SEL = !SEL;
                TF = 1;
            }
            i = 0;
        }
        if (USB1 != Usb1State)
        {
            if (USB1 == 0)
                SEL = 1;
            else
                SEL = 0;
            Usb1State = USB1;
        }
        if (USB2 != Usb2State)
        {
            if (USB2 == 0)
                SEL = 0;
            else
                SEL = 1;
            Usb2State = USB2;
        }
        if (USB1 == 0)
            SEL = 1;
        if (USB2 == 0)
            SEL = 0;
    }
}
